<?php

/**

 * 店铺逻辑层

 * @author maniac

 */

namespace app\common\logic;

use \think\Db;
use think\facade\Session;
use app\facade\UserLogs;
use think\facade\Log As Log;
use app\facade\Options As OptionsFacade;
use app\facade\Salesmans;
use app\facade\Aes AS AesFacade;
use app\common\model\Verify AS VerifyModel;
use app\common\model\Members;
use app\common\model\Shops AS ShopsModel;

class Shops extends Base

{
	
	/**
	
	
	
	* 批量设置自定义浏览量
	
	
	
	* @param  array $data 请求的信息
	
	
	
	*/
	
	
	
	public function listOrder($data)
	
	
	
	{
	
	
		
		if (isset($data['list_order']) && !empty($data['list_order'])) {
	
	
	
			$update = [];
	
	
	
			foreach ($data['list_order'] as $k => $v) {
	
	
	
				$update[] = [
	
	
	
				'id'         => $k,
	
	
	
				'diy_browse_nums' => $v,
	
	
	
				];
	
// 				log::write($data,'notice');
	
			}
	
	
	
			$class = $this->getModel();
	
	
	
			$model = new $class;
	
	
	
			if ($model->saveAll($update)) {
	
	
	
				UserLogs::insertLog(['title' => '批量设置自定义浏览量'.$model->tableName]);
	
	
	
				return true;
	
	
	
			}
	
	
	
		}
	
	
	
		return false;
	
	
	
	}
	/**
     * 重组提交信息
     * @param  array $data 请求的信息
     */
    public function recombine($data)
    {    	
        $prefix = session_prefix();
        
    	$role_id=Session::get('role_id',$prefix);

    	$r=Db::name('users')->where('shop_id',$data['id'])->find();

    	$shops = ShopsModel::field(true)->where('id',$data['id'])->find();
    	
    	if ($role_id == 1) {
    		
    		if(!empty($data['account'])){
    		
    			if (!preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_]{5,}$/', $data['account'])) {
    		
    				exception('请输入正确的用户名');
    		
    			}
    			$where[] = [
    			
    				['username','eq', $data['account']],
    				
    				['shop_id','neq',$data['id']],
    			];
    			
    			$users=Db::name('users')->where($where)->find();
    			
    			if(!empty($users)){
    			
    				exception('当前用户名已存在');
    			
    			}
    			
    		}
    		
    		if (!empty($data['name'])) {
    			
    			$wheres[] = [
    			
    				['name','eq',$data['name']],
    			
    				['id','neq',$data['id']],
    			 
    			];
    			 
    			$shopss = ShopsModel::where($wheres)->find();
    			 
    			if(!empty($shopss)){
    				 
    				exception ('当前店铺名称已存在，请重新输入');
    				    	
    			}
    			
    		}
    		
    		if (isset($data['password']) && $data['password']) {

    			$password = $data['password'];
    			
    			$data = $this->validPass($data);
    			
    		
    		}else {
               unset($data['password']);
            }
    		
    		$shop = $data;
    		
    		if (empty($r)) {
    			
    			Db::name('users')
    			
    			->insert([
    			
	    			'username'=>$shop['account'],
	    			
	    			'password'=>$shop['password'],
	    			
	    			'role_id'=>$shop['category_id']+1,
	    			
	    			'status'=>$shop['status'],
	    			
	    			'name'=>$shop['name'],
	    			
	    			'contact'=>$shop['contacts'],
	    			
	    			'contact_phone'=>$shop['phone'],
	    			
	    			'contact_address'=>$shop['address'],
	    			
	    			'salt'=>$shop['salt'],
	    			
	    			'create_time'=>time(),
	    			
	    			'shop_id'=> $shop['id'],
    			
    			]);

    			if ($shop['status'] == 1) {
    			
    				$info['user'] = $shop['account'];
    			
    				$info['psd'] = $password;
    			
    				$mb = "SMS_171187284";
    			
    				$message = '审核通过通知';
    			
    			}elseif ($shop['status'] == 2){
    			
    				$info['user'] = $shop['account'];
    			
    				$info['psd'] = $password;
    			
    				$info['msg'] = $shop['sms_content'];
    			
    				$mb = "SMS_171192196";
    			
    				$message = '审核拒绝通知';
    			
    			}
    			if ($shop['status'] > 0) {
    			
    					$member = Members::field('id,mobile')->where('id',$shops['userid'])->find();
    						
    					$Sms = new \Aliyunduanxin\SmsDemo();
    						
    					$msg = $Sms->sendSms($member['mobile'],$info,$mb,'大朋宏源科技');
    			
//     					Log::write($msg,'notice');
    						
    					if ($msg->Code == 'OK') {
    			
    						$verify = VerifyModel::create([
    			
    								'type'  => 4,
    			
    								'mobile'   => $member['mobile'],
    			
    								'code'  => $message,
    			
    								]);
    			
    					}
    			
    			}
    			
    		}else{
    			if (isset($data['password']) && $data['password']) {
    				/*密码被修改*/
    				$up_user = [
	    				'username'=>$shop['account'],
	    				'password'=>$shop['password'],
	    				'status' => $shop['status'],
	    				'name' => $shop['name'],
	    				'contact' => $shop['contacts'],
	    				'contact_phone' => $shop['phone'],
	    				'contact_address' => $shop['address'],
	    				'salt'=>$shop['salt'],
    				];
    			} else {
    				/*密码没有被修改*/
    				$up_user = [
	    				'username'=>$shop['account'],
	    				'name' => $shop['name'],
	    				'contact' => $shop['contacts'],
	    				'contact_phone' => $shop['phone'],
	    				'contact_address' => $shop['address'],
    				];
    				if (isset($shop['status'])) {
    					$up_user['status'] = $shop['status'];
    				}
    			}   			
    			
    			Db::name('users')->where('shop_id',$data['id'])->update($up_user);
    		
    		}
    		
    		if (isset($shop['status']) && $shop['status'] == 1) {
    			
    			$where = [
    			 
	    			'salesman_id' => $shop['salesman_id'],
	    			
	    			'shop_id' => $shop['id'],
    			 
    			];
    			
    			$sales = Salesmans::info($where);
    			 
    			if (empty($sales)) {
    				 
    				if ($shop['salesman_id'] > 0 ) {
    					 
    					$options = unserialize(OptionsFacade::option('shop_info'));
    					 
    					$balance = $options['extension_shoper'];
    					 
    					$remark = '推广店铺注册佣金';
    					 
    					Salesmans::record($shop['salesman_id'],$balance,$remark,$shop['id'],0);
    					 
    				}
    			}
    			
    		}
    		
	    }else{

	    	$data['status'] = 0;
	    	
	    	if (empty($r)) {
	    		
	    		$up_user =[
	    		
		    		'status' => $data['status'],
		    		
		    		'name' => $data['name'],
		    		
		    		'contact' => $data['contacts'],
		    		
		    		'contact_phone' => $data['phone'],
		    		
		    		'contact_address' => $data['address'],
	    		 
	    		];
	    		
	    		Db::name('users')->where('shop_id',$data['id'])->update($up_user);
	    		
	    	}
	    	$up_category =[
	    	 
	    	'province' => $data['province'],
	    	
	    	'city' => $data['city'],
	    	
	    	'district' => $data['district'],
	    	
	    	];
	    	Db::name('category')->where('shop_id',$data['id'])->update($up_category);
	    	
	    }
	    
	    return $data;
	    
    }
    
    /**
     * 删除
     * @param  array $data 请求的信息
     */
    public function delete($data)
    {
    	// 验证器验证
    	$validateResult = validate_rule($data, 'del');
    	if ($validateResult !== true) exception($validateResult);
    	if (!isset($data['id']) || !$data['id']) {
    		exception('请选择要删除的信息');
    	}
    	$model = $this->getModel();
    	$obj   = new $model;
    	if (property_exists($obj, 'softId') && $obj->softId) {		
    		$model::destroy($data['id']);
    		$models  = 'app\common\model\Users';
    		$objs   = new $models;
    		$where = [['shop_id','in',$data['id']]];
    		$user = $objs::field('id,shop_id')->where($where)->select()->toArray();
    		foreach ($user as $key =>$val){
    			$models::destroy($val['id']);
    		}
    	} else {
    		$model::destroy($data['id'], true);
    	}
    	$id = is_array($data['id']) ? implode(',', $data['id']) : $data['id'];
    	
    	UserLogs::insertLog(['title' => '删除'.$obj->tableName, 'content' => "id=".$id]);
    	return true;
    }
    /**
     * 用户密码验证
     * @param  array $data 请求的信息
     */
    private function validPass($data)
    {
        if (!trim($data['password'])) {
            exception('请输入密码');
        } elseif (!preg_match('/^[a-zA-Z0-9_@][a-zA-Z0-9_@]{5,}$/', $data['password'])) {
            exception('请输入正确的密码');
        }

        if (!trim($data['password_confirm'])) {
            exception('请输入确认密码');
        } elseif ($data['password_confirm'] != $data['password']) {
            exception('两次密码必须相同');
        }

        $data['salt'] = unid(8);
        $data['password'] = salt_pass($data['password'], $data['salt']);
        return $data;
    }
}

